using System;
using System.Collections.Generic;
using System.Linq;
using ConsentManagementProviderLib.Observer;
using UnityEngine;

namespace ConsentManagementProviderLib
{
	public static class CMP
	{
		private static GameObject mainThreadBroadcastEventsExecutor;

		private static bool IsEditor
		{
			get
			{
				if (Application.platform != RuntimePlatform.LinuxEditor && Application.platform != RuntimePlatform.WindowsEditor)
				{
					return Application.platform == RuntimePlatform.OSXEditor;
				}
				return true;
			}
		}

		public static void Initialize(List<SpCampaign> spCampaigns, int accountId, int propertyId, string propertyName, MESSAGE_LANGUAGE language, CAMPAIGN_ENV campaignsEnvironment, long messageTimeoutInSeconds = 3L)
		{
			IsSpCampaignsValid(spCampaigns);
		}

		public static void LoadMessage(string authId = null)
		{
			if (IsEditor)
			{
				Debug.LogWarning("Emulating LoadMessage call... Sourcepoint CMP works only for real Android/iOS devices, not the Unity Editor.");
			}
		}

		public static void LoadPrivacyManager(CAMPAIGN_TYPE campaignType, string pmId, PRIVACY_MANAGER_TAB tab)
		{
			if (IsEditor)
			{
				Debug.LogWarning($"Emulating LoadPrivacyManager call for {campaignType}... " + "Sourcepoint CMP works only for real Android/iOS devices, not the Unity Editor.");
			}
		}

		public static void CustomConsentGDPR(string[] vendors, string[] categories, string[] legIntCategories, Action<GdprConsent> onSuccessDelegate)
		{
		}

		public static SpConsents GetSpConsents()
		{
			return null;
		}

		public static GdprConsent GetCustomConsent()
		{
			return null;
		}

		public static void Dispose()
		{
		}

		private static void CreateBroadcastExecutorGO()
		{
			if (!(mainThreadBroadcastEventsExecutor != null))
			{
				mainThreadBroadcastEventsExecutor = new GameObject();
				mainThreadBroadcastEventsExecutor.AddComponent<BroadcastEventsExecutor>();
			}
		}

		private static void RemoveIos14SpCampaign(ref List<SpCampaign> spCampaigns)
		{
			List<SpCampaign> list = spCampaigns.Where((SpCampaign campaign) => campaign.CampaignType == CAMPAIGN_TYPE.IOS14).ToList();
			if (list == null || list.Count <= 0)
			{
				return;
			}
			Debug.LogWarning("ios14 campaign is not allowed in non-ios device! Skipping it...");
			foreach (SpCampaign item in list)
			{
				spCampaigns.Remove(item);
			}
		}

		private static bool IsSpCampaignsValid(List<SpCampaign> spCampaigns)
		{
			if (spCampaigns.Count == 0)
			{
				Debug.LogError("You should add at least one SpCampaign to use CMP! Aborting...");
				return false;
			}
			if (IsEditor)
			{
				Debug.LogWarning("ATTENTION! Sourcepoint CMP works only for real Android/iOS devices, not the Unity Editor.");
				return false;
			}
			return true;
		}
	}
}
